﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic;
using Common;

namespace Presentation {
  /// <summary>
  /// Chang password form
  /// </summary>
  public partial class ChangePasswordForm : Form {

    UserBll userBll = new UserBll( );

    #region - Constructor -

    /// <summary>
    /// Initializes a new instance of the <see cref="ChangePasswordForm"/> class.
    /// </summary>
    public ChangePasswordForm( ) {
      InitializeComponent( );
    }

    #endregion

    #region - Events -

    private void okButton_Click( object sender, EventArgs e ) {
      if ( ValidateChildren( ) ) {
        if ( userBll.AuthenticateCurrentUser( SettingsSingleton.Settings.CurrentUser.LoginName, oldPasswordTextBox.Text ) ) {
          SettingsSingleton.Settings.CurrentUser.Password = newPasswordTextBox.Text;
          userBll.SaveUserPassword( SettingsSingleton.Settings.CurrentUser );
          this.Close( );
          MessageBox.Show( "Paswoord gewijzigd." );
        }
      }
    }

    private void textBox_TextChanged( object sender, EventArgs e ) {
      extendedErrorProvider.SetError( ( TextBox ) sender, "" );
    }

    private void ChangePasswordForm_FormClosing( object sender, FormClosingEventArgs e ) {
      e.Cancel = false;
    }

    #endregion

    #region - Validation -

    private void oldPasswordTextBox_Validating( object sender, CancelEventArgs e ) {
      extendedErrorProvider.SetError( oldPasswordTextBox, "" );
      if ( string.IsNullOrEmpty( oldPasswordTextBox.Text ) ) {
        extendedErrorProvider.SetError( oldPasswordTextBox, "Oud paswoord verplicht." );
        e.Cancel = true;
      } else {
        if ( !userBll.AuthenticateCurrentUser( SettingsSingleton.Settings.CurrentUser.LoginName, oldPasswordTextBox.Text ) ) {
          extendedErrorProvider.SetError( oldPasswordTextBox, "Ongeldig paswoord." );
          e.Cancel = true;
        }
      }
    }

    private void newPasswordTextBox_Validating( object sender, CancelEventArgs e ) {
      extendedErrorProvider.SetError( newPasswordTextBox, "" );
      if ( string.IsNullOrEmpty( newPasswordTextBox.Text ) ) {
        extendedErrorProvider.SetError( newPasswordTextBox, "Nieuw paswoord verplicht." );
        e.Cancel = true;
      }
    }

    private void ConfirmPasswordTextBox_Validating( object sender, CancelEventArgs e ) {
      extendedErrorProvider.SetError( confirmPasswordTextBox, "" );
      if ( string.IsNullOrEmpty( confirmPasswordTextBox.Text ) ) {
        extendedErrorProvider.SetError( confirmPasswordTextBox, "Bevestig paswoord verplicht." );
        e.Cancel = true;
      } else {
        if ( confirmPasswordTextBox.Text != newPasswordTextBox.Text ) {
          extendedErrorProvider.SetError( confirmPasswordTextBox, "Paswoord niet gelijk." );
          e.Cancel = true;
        }
      }

    }

    #endregion

  }
}
